ASPECT TECHNIQUE (IMPORTATION DES LIBRAIRES ET CONNEXION A NOTRE
SERVER SQL)
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ✔ readr 2.1.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(DT)
## Warning: package 'DT' was built under R version 4.3.3
library(DBI)
## Warning: package 'DBI' was built under R version 4.3.3
library(odbc)
library(datamodelr)
library(DiagrammeR)
## Warning: package 'DiagrammeR' was built under R version 4.3.3
library(writexl)
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(shiny)
## Warning: package 'shiny' was built under R version 4.3.3
##
## Attaching package: 'shiny'
##
## The following objects are masked from 'package:DT':
##
## dataTableOutput, renderDataTable
library(geosphere)
## Warning: package 'geosphere' was built under R version 4.3.3
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
##
## Attaching package: 'geosphere'
##
## The following object is masked from 'package:shiny':
##
## span
library(shinyjs)
## Warning: package 'shinyjs' was built under R version 4.3.3
##
## Attaching package: 'shinyjs'
##
## The following object is masked from 'package:shiny':
##
## runExample
##
## The following object is masked from 'package:odbc':
##
## show
##
## The following object is masked from 'package:DBI':
##
## show
##
## The following object is masked from 'package:lubridate':
##
## show
##
## The following objects are masked from 'package:methods':
##
## removeClass, show
library(openxlsx)
## Warning: package 'openxlsx' was built under R version 4.3.3
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
# Connexion à la base de données OSP_DATASTAT
con_osp_datastat <- DBI::dbConnect(odbc::odbc(),
Database = "OSP_DATASTAT",
Driver = Sys.getenv("sql_driver"),
Server = Sys.getenv("sql_server"),
UID = Sys.getenv("sql_uid"),
PWD = Sys.getenv("sql_pwd"),
Port = Sys.getenv("sql_port"))
I - PORTRAIT DE NOS CLIENTS : QUI SONT-ILS ET QUELLES SONT LEURS
CARACTÉRISTIQUES ?
1- Ospharm - Datastat
Actif_table <- dbGetQuery(con_osp_datastat,"
WITH Liste_Jours AS (
SELECT CAST('2002-01-01' AS DATE) AS date_adhesion
UNION ALL
SELECT DATEADD(DAY, 1, date_adhesion)
FROM Liste_Jours
WHERE date_adhesion < CAST(GETDATE() AS DATE))
SELECT FORMAT(Liste_Jours.date_adhesion, 'yyyy/MM/dd') AS date_complete,
COALESCE(COUNT(a.n_auto_adhpha), 0) AS Nb_Actif_journalier,
SUM(COALESCE(COUNT(a.n_auto_adhpha), 0)) OVER (ORDER BY Liste_Jours.date_adhesion) AS somme_cumulative_Actif
FROM Liste_Jours
LEFT JOIN [OSP_PROD].[dbo].os_adhpha a
ON CAST(a.dinscript_adhpha AS DATE) = Liste_Jours.date_adhesion
WHERE a.dextinct_adhpha IS NULL
GROUP BY Liste_Jours.date_adhesion
ORDER BY Liste_Jours.date_adhesion
OPTION (MAXRECURSION 0)")
Actif_table <- as_tibble(Actif_table)
# Historique des adhésions pour les inactifs
Inactif_table <- dbGetQuery(con_osp_datastat,"
WITH Liste_Jours AS (
SELECT CAST('2002-01-01' AS DATE) AS date_Retrait
UNION ALL
SELECT DATEADD(DAY, 1, date_Retrait)
FROM Liste_Jours
WHERE date_Retrait < CAST(GETDATE() AS DATE)
)
SELECT
FORMAT(Liste_Jours.date_Retrait, 'yyyy/MM/dd') AS date_complete,
COALESCE(COUNT(a.n_auto_adhpha), 0) AS Nb_Inactif_journalier,
SUM(COALESCE(COUNT(a.n_auto_adhpha), 0)) OVER (ORDER BY Liste_Jours.date_Retrait) AS somme_cumulative_inactif
FROM Liste_Jours
LEFT JOIN [OSP_PROD].[dbo].os_adhpha a
ON CAST(a.dextinct_adhpha AS DATE) = Liste_Jours.date_Retrait
WHERE a.dextinct_adhpha IS NOT NULL
GROUP BY Liste_Jours.date_Retrait
ORDER BY Liste_Jours.date_Retrait
OPTION (MAXRECURSION 0)")
Inactif_table <- as_tibble(Inactif_table)
# Fusion des deux tables
Adhesion_table <- Inactif_table %>%
left_join(Actif_table, by = "date_complete") %>%
select(date_complete, somme_cumulative_Actif, somme_cumulative_inactif)
# Convertir les formats de données
Adhesion_table <- Adhesion_table %>%
mutate(
date_complete = as.Date(date_complete, format = "%Y/%m/%d"),
somme_cumulative_Actif = as.numeric(somme_cumulative_Actif),
somme_cumulative_inactif = as.numeric(somme_cumulative_inactif))
view(Adhesion_table)
##### graphique #########
# Ajouter une colonne "année" pour regrouper les données par année
Adhesion_table <- Adhesion_table %>%
mutate(annee = format(date_complete, "%Y"))
# Récupérer les dernières valeurs de chaque année en fonction des actifs
Adhesion_cumul_annuel <- Adhesion_table %>%
group_by(annee) %>%
filter(date_complete == date_complete[which.max(somme_cumulative_Actif)]) %>% # Date correspondant au max des actifs
ungroup()
# Convertir la colonne "année" en format numérique pour l'axe X
Adhesion_cumul_annuel$annee <- as.numeric(Adhesion_cumul_annuel$annee)
# Calculer l'évolution absolue et en pourcentage entre deux années consécutives
Adhesion_cumul_annuel <- Adhesion_cumul_annuel %>%
arrange(annee) %>%
mutate(
evolution_Actif_absolu = somme_cumulative_Actif - lag(somme_cumulative_Actif),
evolution_Inactif_absolu = somme_cumulative_inactif - lag(somme_cumulative_inactif),
evolution_Actif_pourcentage = (evolution_Actif_absolu - lag(evolution_Actif_absolu)) / abs(lag(evolution_Actif_absolu)) * 100,
evolution_Inactif_pourcentage = (evolution_Inactif_absolu - lag(evolution_Inactif_absolu)) / abs(lag(evolution_Inactif_absolu)) * 100
)
# Création du graphique interactif avec courbes et étiquettes fixes
fig <- plot_ly(Adhesion_cumul_annuel, x = ~annee) %>%
# Courbe des actifs
add_trace(
y = ~somme_cumulative_Actif,
type = 'scatter',
mode = 'lines+markers', # Lignes et marqueurs
name = 'Actif',
line = list(color = 'blue', shape = 'spline'), # Courbe lissée (spline)
marker = list(size = 6),
hoverinfo = 'text',
text = ~paste(
'Année:', annee,
'<br>Actifs:', somme_cumulative_Actif,
'<br>Évolution absolue:', ifelse(is.na(evolution_Actif_absolu), "N/A", evolution_Actif_absolu),
'<br>Évolution %:', ifelse(is.na(evolution_Actif_pourcentage), "N/A", paste0(round(evolution_Actif_pourcentage, 2), "%"))
)
) %>%
# Courbe des inactifs
add_trace(
y = ~somme_cumulative_inactif,
type = 'scatter',
mode = 'lines+markers', # Lignes et marqueurs
name = 'Inactif',
line = list(color = 'red', shape = 'spline'), # Courbe lissée (spline)
marker = list(size = 6),
hoverinfo = 'text',
text = ~paste(
'Année:', annee,
'<br>Inactifs:', somme_cumulative_inactif,
'<br>Évolution absolue:', ifelse(is.na(evolution_Inactif_absolu), "N/A", evolution_Inactif_absolu),
'<br>Évolution %:', ifelse(is.na(evolution_Inactif_pourcentage), "N/A", paste0(round(evolution_Inactif_pourcentage, 2), "%"))
)
) %>%
# Ajouter des étiquettes fixes sur la courbe des actifs
add_trace(
y = ~somme_cumulative_Actif,
type = 'scatter',
mode = 'text', # Étiquettes fixes
text = ~paste('', somme_cumulative_Actif),
textposition = "top middle",
showlegend = FALSE
) %>%
# Ajouter des étiquettes fixes sur la courbe des inactifs
add_trace(
y = ~somme_cumulative_inactif,
type = 'scatter',
mode = 'text', # Étiquettes fixes
text = ~paste('', somme_cumulative_inactif),
textposition = "top middle",
showlegend = FALSE
) %>%
layout(
title = "Courbe d'évolution de mon Panel avec étiquettes corrigées",
xaxis = list(title = "Année"),
yaxis = list(title = "Effectifs des Actifs et Inactifs"),
legend = list(orientation = "h", x = 0.4, y = -0.2)
)
# Afficher le graphique
fig
# L’examen du graphique met en évidence une évolution constante du nombre d’adhérents Datastat depuis 2002 jusqu’à aujourd’hui. Bien que cette progression soit marquée par une tendance générale à la hausse, on observe par moments des fluctuations où les adhésions enregistrées sont légèrement inférieures aux périodes précédentes. Cette croissance exponentielle peut être attribuée à la signature de nouveaux partenariats avec de grands groupements, qui ont largement contribué à renforcer la notoriété et l’attractivité de Datastat.
#Cependant, il est également important de souligner que parallèlement à cette augmentation du nombre d’adhérents actifs, le nombre d’inactifs au sein de Datastat ne cesse de croître. Cette situation peut s’expliquer par plusieurs facteurs, notamment :
# - La méconnaissance de certains clients concernant les fonctionnalités et les avantages offerts par le site Datastat.
# - Les fermetures temporaires ou définitives de certaines officines, qui influent directement sur la proportion d’utilisateurs actifs.
# - Des défis liés à la réactivation des anciens adhérents ou à leur engagement continu dans l’utilisation des services.
# Conclusion: Ces dynamiques soulignent l’importance de développer des stratégies spécifiques pour améliorer l’engagement des adhérents et réduire le taux d’inactivité, tout en continuant à capitaliser sur la croissance issue des nouveaux partenariats. officines.